﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class Search_emp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string search;
            string name = TextBox1.Text;
            string surname = TextBox2.Text;
            if (name == "" && surname != "")
            {
                search = @"select Personnel_name , Personnel_surname , Positions_name ,Personnel_id
                            from Table_Personnel
                            inner join Table_positions
                                on Table_Personnel.Positions_id = Table_positions.Positions_id
                            where Personnel_surname like '%" + surname + "%' order by Table_Personnel.Positions_id";
            }
            else if (name != "" && surname == "")
            {
                search = @"select Personnel_name , Personnel_surname , Positions_name ,Personnel_id
                            from Table_Personnel
                            inner join Table_positions
                                on Table_Personnel.Positions_id = Table_positions.Positions_id
                            where Personnel_name like '%" + name + "%' order by Table_Personnel.Positions_id";                            
            }
            else if (name != "" && surname != "")
            {
                search = @"select Personnel_name , Personnel_surname , Positions_name ,Personnel_id
                            from Table_Personnel
                            inner join Table_positions
                                on Table_Personnel.Positions_id = Table_positions.Positions_id
                            where Personnel_name like '%" + name + "%' and Personnel_surname like '%" + surname + "%' order by Table_Personnel.Positions_id";
            }
            else
            {
                search = @"select Personnel_name , Personnel_surname , Positions_name ,Personnel_id
                            from Table_Personnel
                            inner join Table_positions
                                on Table_Personnel.Positions_id = Table_positions.Positions_id
                            where Personnel_name = '" + name + "' and Personnel_surname = '" + surname + "'";
            }
            SqlCommand sqlcomm = new SqlCommand();
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(search, sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string search = @"select Personnel_name , Personnel_surname , Positions_name ,Personnel_id
                                from Table_Personnel
                                inner join Table_positions
                                    on Table_Personnel.Positions_id = Table_positions.Positions_id
                                order by Table_Personnel.Positions_id";
            SqlCommand sqlcomm = new SqlCommand();
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(search, sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdY")
            {
                Session["emp_id"] = e.CommandArgument.ToString();
                Response.Redirect("Detail_emp_admin.aspx");
            }
        }
    }
}